package org.tau.workshop2011.parsing.AST

import org.tau.workshop2011.expressions.Type

abstract class Attribute (val leftSide:Variable, val value:Option[Expr]) extends ASTNode {
  val name = leftSide.id

  def getName : String = name
  def getLeftType : Type = leftSide.resultType
  def getRightType : Type = value match {
      case Some(value) => value.resultType
      case None => Type.tUnknown
    }
  def getValue : Option[Expr] = value

  def autoResolveType {
    value match {
      case Some(value) => value.autoResolveType
      case None =>
    }

    val left = getLeftType
    val right = getRightType
    (left, right) match {
      case (Type.tUnknown, Type.tUnknown) =>
      case (_, Type.tUnknown) => value match {
        case Some(value) => value.resolveTypeTo(left)
        case None =>
      }
      case (Type.tUnknown, _) => leftSide resolveTypeTo right
      case (_, _) => if (left != right) throw new Exception ("Assignment to " +
        leftSide + " (of type " + left + ") from incompatiable type " + right)
    }
  }

  def resolveTypeTo (newType:Type) {
    leftSide resolveTypeTo newType
    value match {
      case Some(value) => value resolveTypeTo newType
      case None =>
    }
  }
}